package com.jboom.templates.statemachine.java.statemachine;

import com.google.common.collect.HashBasedTable;

import java.util.List;

/**
 * @author zhangwen
 * @date 2023/5/26 9:07
 */
public abstract class AbstractStateMachine {

    protected HashBasedTable<State, Event, StateProcess> processMap = HashBasedTable.create();

    {
        List<StateProcess> processes = init();
        processes.forEach(stateProcess -> {
            processMap.put(stateProcess.getFrom(), stateProcess.getEvent(), stateProcess);
        });
    }

    protected abstract List<StateProcess> init();

    public State getNextState(State state, Event event) {
        StateProcess stateProcess = processMap.get(state, event);
        return stateProcess.getTo();
    }

    public Action getAction(State state, Event event) {
        StateProcess stateProcess = processMap.get(state, event);
        return stateProcess.getAction();
    }
}
